package future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author fei
 * @date 2023/1/6 10:59
 * @description 线程池说明
 */
public class CompletableFutureWithThreadPoolDemo {

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        m1(threadPool);
        m2(threadPool);
        m3(threadPool);
    }

    /**
     * 有可能处理太快，系统优化切换原则，直接使用 main 线程处理
     * @param threadPool
     */
    private static void m3(ExecutorService threadPool) {
        try{
            CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
                // try{ TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("1号任务" + "\t" + Thread.currentThread().getName());
                return "abcd";
            }, threadPool).thenRun(() -> {
                // try{ TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("2号任务" + "\t" + Thread.currentThread().getName());
            }).thenRun(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("3号任务" + "\t" + Thread.currentThread().getName());
            }).thenRun(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("4号任务" + "\t" + Thread.currentThread().getName());
            });

            System.out.println(completableFuture.get(2L, TimeUnit.SECONDS));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

    /**
     * 使用自定义线程池
     * 调用 thenRun 方法执行第二个任务时，则第二个任务和第一个任务时共用同一个线程池
     * 调用 thenRunAsync 执行第二个任务时，则第一个任务使用的是你自己传入的线程池，第二个任务使用的是 ForkJoin 线程池
     * @param threadPool
     */
    private static void m2(ExecutorService threadPool) {
        try{
            CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("1号任务" + "\t" + Thread.currentThread().getName());
                return "abcd";
            }, threadPool).thenRun(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("2号任务" + "\t" + Thread.currentThread().getName());
            }).thenRunAsync(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("3号任务" + "\t" + Thread.currentThread().getName());
            }).thenRun(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("4号任务" + "\t" + Thread.currentThread().getName());
            });

            System.out.println(completableFuture.get(2L, TimeUnit.SECONDS));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

    /**
     * 没有传入自定义线程池，都用默认线程池 ForkJoinPool
     * @param threadPool
     */
    private static void m1(ExecutorService threadPool) {
        try{
            CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("1号任务" + "\t" + Thread.currentThread().getName());
                return "abcd";
            }).thenRun(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("2号任务" + "\t" + Thread.currentThread().getName());
            }).thenRun(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("3号任务" + "\t" + Thread.currentThread().getName());
            }).thenRun(() -> {
                try{ TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println("4号任务" + "\t" + Thread.currentThread().getName());
            });

            System.out.println(completableFuture.get(2L, TimeUnit.SECONDS));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

}
